<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
<!--    卡片视图区域-->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
<!--      表格区域-->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{scope.row.create_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditOrder">编辑</el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="showLocationOrder(scope.row.order_id)">位置</el-button>
          </template>
        </el-table-column>
      </el-table>
<!--      分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
<!--    编辑对话框-->
    <el-dialog
      title="编辑地址"
      :visible.sync="editDialogVisible"
      width="50%">
      <el-form :model="editRuleForm" :rules="editRuleFormRules" ref="editRuleFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
<!--          级联选择框-->
          <el-cascader
            v-model="editRuleForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editRuleForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
<!--    位置对话框-->
    <el-dialog
      title="查看物流进度"
      :visible.sync="locationDialogVisible"
      width="50%">
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in locationList"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
    <el-button @click="locationDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="locationDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  name: 'Order',
  data () {
    return {
      queryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        pagesize: 5
      },
      inputValue: '',
      // 保存获取订单列表的数据对象
      orderList: [],
      // 订单列表总数据条数
      total: 0,
      // 编辑对话框状态
      editDialogVisible: false,
      // 表单绑定数据对象
      editRuleForm: {
        address1: [],
        address2: ''
      },
      // 表单绑定验证规则
      editRuleFormRules: {
        address1: [
          { required: true, message: '请选择地址名称', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 城市数据
      cityData,
      // 物流进度对话框
      locationDialogVisible: false,
      // 物流数据
      locationList: [],
      // 物流进度为倒序
      reverse: false
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      console.log(res)
      // 获取的数据赋值给orderList
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 订单列表当前也展示条数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 订单列表页面切换
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 点击编辑弹出对话框
    showEditOrder () {
      this.editDialogVisible = true
    },
    // 编辑订单列表级联选择
    handleChange () {},
    // 点击位置操作
    async showLocationOrder (orderId) {
      const { data: res } = await this.$http.get(`/kuaidi/${orderId}`)
      if (res.meta.status !== 200) {
        return this.$message('获取数据失败')
      }
      this.locationList = res.data
      console.log(res.data)
      this.locationDialogVisible = true
    }
  }
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>
